import QtQuick 2.14
import QtQuick.Window 2.14
import QtQuick.Layouts 1.14
import QtQuick.Controls 2.14
import QtQuick.Controls.Material 2.14
import QtQuick.Shapes 1.14
import QtQuick.Dialogs 1.0
import Qt.labs.settings 1.0
import "./Fonts"
import my.CServiceBackend 1.0

ApplicationWindow {
    property color backGroundColor: "#20b2aa"
    property var tikuNum: {"单选题":0,"多选题":0,"判断题":0,"填空题":0,"简答题":0}
    property var appDir: ""
    //试卷生成过程中是否存在错误或警告信息
    property bool hasError: false //是否存在影响打印的错误信息
    property bool hasWarning: false //是否存在需要提醒用户注意的信息
    //添加设置页面的属性
    property var pageSettingMap:{
        "上边距":38,"下边距":35,"左边距":27,"右边距":27,
        "主标题字体":"方正小标宋简体","主标题字号":22,
        "一级标题字体":"黑体","一级标题字号":16,
        "正文字体":"仿宋_GB2312","正文字号":16,
        "考生信息字体":"仿宋_GB2312","考生信息字号":14,
        "行间距":8,"分辨率":300,"横向页面":false
    }
    property var totalScore: 0 //总分值

    visible: true
    width: 600
    height: 600
    minimumWidth: 300
    Material.background: backGroundColor
    title: qsTr("试卷自动生成系统")
    //自定义组件
    CServiceBackend{
        id:serviceBackend
        onError: {
            var errorMsg = getError();
            messageBox.close();
            messageBox.text = errorMsg;
            messageBox.style = "danger";
            timer.stop();
            messageBox.open();
        }
        onReadReady: {
            tikuNum = getItemBankMsg();//获取题库信息
            messageBox.close();
            messageBox.text = "导入题库成功！";
            messageBox.style = "sucess";
            if(!timer.running){
                timer.start();

            }
            else{
                timer.stop();
                timer.start();
            }

            messageBox.open();
        }

    }

    //函数区
    function readFileAndCheck(url){//读取文件路径，判断是否为有效题库，如果有效则导入各项设置
        //获取文件路径
        let temp_filePath = url;
        filePath.text = temp_filePath.slice(8);//去除了"file:///"头
        //保存文件路径
        let folderPath = getFolderPath(temp_filePath);//需要包含"file:///"头
        settingsMain.setValue("importFoldPath",folderPath);
        //设置后台的m_excelFileName变量
        serviceBackend.setExcelFileName(filePath.text);
        let ret = serviceBackend.readItemBankMsg();
        if(!ret){
            //读取失败，则将题库信息清零
            tikuNum ={"单选题":0,"多选题":0,"判断题":0,"填空题":0,"简答题":0};
            bt_printTestPaper.enabled = false;
            //清空试卷默认标题
            paperTitle.text = "";
            filePath.text = "";
        }else{
            bt_printTestPaper.enabled = true;
            //生成试卷默认标题
            paperTitle.text = getFileName(filePath.text)+"理论试卷";
            updateZuJuanXinxi();
        }
    }

    function openFileAccept(){
        readFileAndCheck(myLoader.item.fileUrl.toString());
        //取消信号连接，释放资源
        myLoader.item.onAccepted.disconnect(openFileAccept);
        myLoader.setSource("");

    }
    //通过文件路径，读取文件名
    function getFileName(filePath){
        let temp_array = filePath.split('/');
        let temp_string = temp_array[temp_array.length-1];
        let fileName = temp_string.split('.')[0];
        return fileName;
    }
    //获取文件夹路径
    function getFolderPath(filePath){
        let temp_array = filePath.split('/');
        //获取带后缀的文件名
        let tempName = temp_array[temp_array.length-1];
        return filePath.slice(0,filePath.length - tempName.length -1);
    }

    //更新组卷信息、
    function updateZuJuanXinxi(){
        let temp_totalNum = 0;//总题数
        let temp_totalScore = 0;//总分值
        //计算每种题型的数量和分值
        //单选题
        //1.题库数量为0；2.出题数量为0或为空；3.出题分值为0或为空，以上情况不计如总题述和总分值
        if(tikuNum["单选题"]!==0 && parseInt(dnum.text)!==0 && dnum.text!="" && dfenzhi.text !="" && parseFloat(dfenzhi.text)!==0){
            temp_totalNum += parseInt(dnum.text);
            temp_totalScore += parseInt(dnum.text)*parseFloat(dfenzhi.text);
        }
        //多选题
        if(tikuNum["多选题"]!==0  && dxnum.text!="" && dxfenzhi.text !=""){
            temp_totalNum += parseInt(dxnum.text);
            temp_totalScore += parseInt(dxnum.text)*parseFloat(dxfenzhi.text);
        }
        //判断题
        if(tikuNum["判断题"]!==0  && pnum.text!="" && pfenzhi.text !=""){
            temp_totalNum += parseInt(pnum.text);
            temp_totalScore += parseInt(pnum.text)*parseFloat(pfenzhi.text);
        }
        //填空题
        if(tikuNum["填空题"]!==0  && tnum.text!="" && tfenzhi.text !=""){
            temp_totalNum += parseInt(tnum.text);
            temp_totalScore += parseInt(tnum.text)*parseFloat(tfenzhi.text);
        }

        //简答题
        if(tikuNum["简答题"]!==0  && jnum.text!="" && jfenzhi.text !=""){
            temp_totalNum += parseInt(jnum.text);
            temp_totalScore += parseInt(jnum.text)*parseFloat(jfenzhi.text);
        }
        totalScore = temp_totalScore;
        txt_zuJuanXinXi.text = "本试卷共"+temp_totalNum+"题，总分值"+totalScore+"分";
    }
    //检查组卷信息正确性
    function checkZuJuan(){
        //通过txt_errorMsg.text,txt_warningMsg.text,设置错误、警告信息
        //通过hasError,hasWarning，确定组题设置中的错误
        let i = 0;//提示信息序号
        let errorMsg ="";//临时保存错误信息
        let warningMsg = "";//临时保存警告信息
        hasError = false;
        hasWarning = false;

        //第一类警告信息，出题数量超出题库数量
        if(parseInt(dnum.text)>tikuNum["单选题"] && tikuNum["单选题"]!==0){
            //单选题出题数量超过题库数量，且题库数量不为0，时进行警告
            hasWarning = true;
            i +=1;
            if(i>1)//第二行开始要换行
                warningMsg += "\n";
            warningMsg += "    "+i+".单选题出题数量超过题库中试题数量，试卷中将出现重复试题！";
        }
        if(parseInt(dxnum.text)>tikuNum["多选题"] && tikuNum["多选题"]!==0){
            //多选题出题数量超过题库数量，且题库数量不为0，时进行警告
            hasWarning = true;
            i +=1;
            if(i>1)//第二行开始要换行
                warningMsg += "\n";
            warningMsg += "    "+i+".多选题出题数量超过题库中试题数量，试卷中将出现重复试题！";
        }
        if(parseInt(pnum.text)>tikuNum["判断题"] && tikuNum["判断题"]!==0){
            //判断题出题数量超过题库数量，且题库数量不为0，时进行警告
            hasWarning = true;
            i +=1;
            if(i>1)//第二行开始要换行
                warningMsg += "\n";
            warningMsg += "    "+i+".判断题出题数量超过题库中试题数量，试卷中将出现重复试题！";
        }
        if(parseInt(tnum.text)>tikuNum["填空题"] && tikuNum["填空题"]!==0){
            //填空题出题数量超过题库数量，且题库数量不为0，时进行警告
            hasWarning = true;
            i +=1;
            if(i>1)//第二行开始要换行
                warningMsg += "\n";
            warningMsg += "    "+i+".填空题出题数量超过题库中试题数量，试卷中将出现重复试题！";
        }
        if(parseInt(jnum.text)>tikuNum["简答题"] && tikuNum["简答题"]!==0){
            //简答题出题数量超过题库数量，且题库数量不为0，时进行警告
            hasWarning = true;
            i +=1;
            if(i>1)//第二行开始要换行
                warningMsg += "\n";
            warningMsg += "    "+i+".简答题出题数量超过题库中试题数量，试卷中将出现重复试题！";
        }
        //第二类警告信息，总分不是100分
        if(totalScore != 100){
            //总分值不为100分
            hasWarning = true;
            i +=1;
            if(i>1)//第二行开始要换行
                warningMsg += "\n";
            warningMsg += "    "+i+".总分值不为100分";
        }
        //第三类警告信息，题库没有该题型，但我们设置了，提示不会出现该题型
        if(tikuNum["单选题"]==0 && parseInt(dnum.text)>0){
            //题库中没有单选题，但设置的单选题
            hasWarning = true;
            i +=1;
            if(i>1)//第二行开始要换行
                warningMsg += "\n";
            warningMsg += "    "+i+".题库中没有单选题，您设置的单选题不会出现！";
        }
        if(tikuNum["多选题"]==0 && parseInt(dxnum.text)>0){
            //题库中没有多选题，但设置的多选题
            hasWarning = true;
            i +=1;
            if(i>1)//第二行开始要换行
                warningMsg += "\n";
            warningMsg += "    "+i+".题库中没有多选题，您设置的多选题不会出现！";
        }
        if(tikuNum["判断题"]==0 && parseInt(pnum.text)>0){
            //题库中没有判断题，但设置的判断题
            hasWarning = true;
            i +=1;
            if(i>1)//第二行开始要换行
                warningMsg += "\n";
            warningMsg += "    "+i+".题库中没有判断题，您设置的判断题不会出现！";
        }
        if(tikuNum["填空题"]==0 && parseInt(tnum.text)>0){
            //题库中没有填空题，但设置的填空题
            hasWarning = true;
            i +=1;
            if(i>1)//第二行开始要换行
                warningMsg += "\n";
            warningMsg += "    "+i+".题库中没有填空题，您设置的填空题不会出现！";
        }
        if(tikuNum["简答题"]==0 && parseInt(jnum.text)>0){
            //题库中没有简答题，但设置的简答题
            hasWarning = true;
            i +=1;
            if(i>1)//第二行开始要换行
                warningMsg += "\n";
            warningMsg += "    "+i+".题库中没有简答题，您设置的简答题不会出现！";
        }
        //严重错误
        //试卷标题未设置
        i=0;
        if(paperTitle.text ==="" || paperTitle.text === undefined){
            hasError =true;
            i+=1;
            if(i>1)//第二行开始要换行
                errorMsg += "\n";
            errorMsg +="    "+ i +".试卷标题未设置，请重新设置！"
        }
        //没有有效题型
        if((parseInt(dnum.text)==0 || dnum.text =="" || dfenzhi.text =="" || parseFloat(dfenzhi.text)==0)&& (parseInt(dxnum.text)==0 || dxnum.text =="" || dxfenzhi.text =="" || parseFloat(dxfenzhi.text)==0)&& (parseInt(pnum.text)==0 || pnum.text =="" || pfenzhi.text =="" || parseFloat(pfenzhi.text)==0)&&(parseInt(tnum.text)==0 || tnum.text =="" || tfenzhi.text =="" || parseFloat(tfenzhi.text)==0)&&(parseInt(jnum.text)==0 || jnum.text =="" || jfenzhi.text =="" || parseFloat(jfenzhi.text)==0) ){
            hasError =true;
            i+=1;
            if(i>1)//第二行开始要换行
                errorMsg += "\n";
            errorMsg +="    "+ i +".必须设置至少1种有效题型！"
        }

        txt_errorMsg.text = errorMsg;
        txt_warningMsg.text = warningMsg;

    }
    //打印试卷
    function printTestPaper(){
        //获取试卷标题
        serviceBackend.getTestPaperTittle(paperTitle.text);
        //获取组题设置,先清空原来的设置
        serviceBackend.clearTestPaperSet();
        if(dnum.text !="" && dfenzhi.text!="" && parseInt(dnum.text)!==0 && parseFloat(dfenzhi.text)!==0){
            //单选题的设置不能为空，且数量和分值任何一个不能为零，要不然该题型就没用了
            serviceBackend.appendTestPaperSet("单选题",parseInt(dnum.text),parseFloat(dfenzhi.text));
        }
        if(dxnum.text !="" && dxfenzhi.text!="" && parseInt(dxnum.text)!==0 && parseFloat(dxfenzhi.text)!==0){
            //多选题的设置不能为空，且数量和分值任何一个不能为零，要不然该题型就没用了
            serviceBackend.appendTestPaperSet("多选题",parseInt(dxnum.text),parseFloat(dxfenzhi.text));
        }
        if(pnum.text !="" && pfenzhi.text!="" && parseInt(pnum.text)!==0 && parseFloat(pfenzhi.text)!==0){
            serviceBackend.appendTestPaperSet("判断题",parseInt(pnum.text),parseFloat(pfenzhi.text));
        }
        if(tnum.text !="" && tfenzhi.text!="" && parseInt(tnum.text)!==0 && parseFloat(tfenzhi.text)!==0){
            serviceBackend.appendTestPaperSet("填空题",parseInt(tnum.text),parseFloat(tfenzhi.text));
        }
        if(jnum.text !="" && jfenzhi.text!="" && parseInt(jnum.text)!==0 && parseFloat(jfenzhi.text)!==0){
            serviceBackend.appendTestPaperSet("简答题",parseInt(jnum.text),parseFloat(jfenzhi.text));
        }
        //获取页面设置
        var tempSettingsMap = pageSettingMap;
        tempSettingsMap["双面"] = rb_doubleSide.checked;
        tempSettingsMap["侧面封装"] = rb_side.checked;
        tempSettingsMap["纸张"] = cb_pageSize.currentText;
        serviceBackend.readPageSetting(tempSettingsMap);
        //启动打印程序
        var ret = serviceBackend.printTestPaper();
        if(ret){
            messageBox.close();
            messageBox.text = "试卷生成成功！";
            messageBox.style = "sucess";
            if(!timer.running){
                timer.start();
            }
            else{
                timer.stop();
                timer.start();
            }
            messageBox.open();
        }

    }

    //读取保存的页面设置信息,更新到pageSettingMap中
    function readPageSetting(){
        //将设置字符串信息转化为设置对象
        var tempSetString = settingsPage.value("pageSet");

        var tempObj;
        if(tempSetString === undefined){
            //程序在初始情况下是没有pageSet值的，所以会出现undefined的错误
            tempObj = pageSettingMap;
        }
        else{
            if(tempSetString.length ===0 ){
                //有内容，但为空
                tempObj = pageSettingMap;
            }else {
                //读取到设置字符串，转化为对象
                tempObj = JSON.parse(tempSetString);//把字符串转为对象
                if(tempObj["上边距"] === undefined){//内部数据无效,使用默认设置
                    tempObj = pageSettingMap;
                }
            }

        }
        return tempObj;
    }



    //设置一个标题
    header: Label{
        font.family: "方正小标宋简体"
        font.pointSize: 25
        font.bold: true
        text: "试卷自动生成系统"
        color:"White"
        horizontalAlignment: Qt.AlignHCenter
        topPadding: 20
        bottomPadding: 20
        background: Rectangle{
            color: backGroundColor
        }
    }
    //主界面布局
    Flickable{
        anchors.fill: parent
        topMargin: 10
        contentHeight: columnLayout.height
        ColumnLayout{
            id:columnLayout
//            anchors.fill: parent
            anchors.top: parent.top
            anchors.left: parent.left
            anchors.right: parent.right
            spacing: 10
            //导入题库
            //做一个白色背景
            Rectangle{
                Layout.fillWidth: true
                Layout.leftMargin: 10
                Layout.rightMargin: 10
                implicitHeight: ly1.height+10
                color: "white"
                radius: 5
                GridLayout{
                    id:ly1
                    width: parent.width
                    columns: 2
                    Label{
                        Layout.topMargin: 10
                        Layout.leftMargin: 10
                        Layout.alignment: Qt.AlignLeft
                        Layout.columnSpan :2
                        font.pointSize: 16
                        text:"导入题库"
                    }
                    TextField{
                        id:filePath
                        Layout.leftMargin: 10
                        Layout.fillWidth: true
                        font.pointSize: 12
                        placeholderText: "请导入题库"
                    }
                    Button{
                        Layout.rightMargin: 10
                        Layout.alignment: Qt.AlignRight
                        text: "导入题库"
                        Material.background: Material.Green
                        Material.foreground: "white"
                        onClicked: {
                            myLoader.sourceComponent = openFileDialog;
                            var importFoldPath = settingsMain.value("importFoldPath");
                            if(importFoldPath === undefined || importFoldPath==="")
                                myLoader.item.folder = "file:///c:/"
                            else
                                myLoader.item.folder = settingsMain.value("importFoldPath");
                            myLoader.item.open();
                            myLoader.item.onAccepted.connect(openFileAccept);
                        }
                    }
                }
                DropArea{
                    anchors.fill: parent
                    onDropped: {
                        var url = drop.urls[0];
                        readFileAndCheck(url);
                    }
                }
            }
            //出题设置的界面
            //试卷标题
            Rectangle{
                Layout.fillWidth: true
                Layout.leftMargin: 10
                Layout.rightMargin: 10
                implicitHeight: ly3.height+10
                color: "white"
                radius: 5
                ColumnLayout{
                    id:ly3
                    width: parent.width
                    Label{
                        Layout.topMargin: 10
                        Layout.leftMargin: 10
                        Layout.alignment: Qt.AlignLeft
                        font.pointSize: 16
                        text:"试卷标题"
                    }
                    TextField{
                        id:paperTitle
                        Layout.leftMargin: 10
                        Layout.fillWidth: true
                        Layout.rightMargin: 10
                        font.pointSize: 12
                        selectByMouse: true
                        placeholderText: "试卷标题"
                    }
                }
            }
            //组题设置
            Rectangle{
                Layout.fillWidth: true
                Layout.leftMargin: 10
                Layout.rightMargin: 10
                implicitHeight: ly4.height+10
                color: "white"
                radius: 5
                GridLayout{
                    id:ly4
                    width: parent.width
                    columns :6
                    Label{
                        Layout.topMargin: 10
                        Layout.leftMargin: 10
                        Layout.alignment: Qt.AlignLeft
                        Layout.columnSpan :6
                        font.pointSize: 16
                        text:"组题设置"
                    }
                    //画一条分割线
//                    Rectangle{
//                        Layout.columnSpan :6
//                        Layout.topMargin: 10
//                        Layout.leftMargin: 10
//                        Layout.rightMargin: 10
//                        Layout.fillWidth: true
//                        Layout.preferredHeight: 1
//                        border.width: 1
//                        border.color: "grey"
//                        color: "grey"
//                    }
                    DashLineH{
                        Layout.preferredHeight: 5
                        Layout.columnSpan :6
                        Layout.leftMargin: 10
                        Layout.rightMargin: 10
                        Layout.fillWidth: true
                    }

                    //单选题设置
                    Label{
                        Layout.leftMargin: 10
                        text: "单选题"
                        leftPadding: 10
                        rightPadding: 10
                        topPadding: 5
                        bottomPadding: 5
                        font.pointSize: 14
                        background: Rectangle{
                            color: Material.color(Material.DeepPurple)
                        }
                        Material.foreground: "white"
                    }
                    Label{
                        font.pointSize: 12
                        text: "共"+tikuNum["单选题"]+"题"
                    }
                    TextField{
                        id:dnum
                        Layout.fillWidth: true
                        Layout.leftMargin: 20
                        font.pointSize: 12
                        horizontalAlignment: Text.AlignHCenter
                        selectByMouse: true
                        //限制输入为>=0的整数
                        validator: IntValidator{bottom: 0; }
                        color: parseInt(text)>tikuNum["单选题"] ? "red" : "black"
                        placeholderText: "出题数量"
                        onTextEdited:{
                            updateZuJuanXinxi();
                        }
                    }
                    Label{
                        Layout.alignment: Qt.AlignLeft
                        font.pointSize: 12
                        text:"题"
                    }
                    TextField{
                        id:dfenzhi
                        Layout.fillWidth: true
                        Layout.leftMargin: 20
                        font.pointSize: 12
                        horizontalAlignment: Text.AlignHCenter
                        selectByMouse: true
                        validator:RegExpValidator{
                            regExp: /\d(\.\d)?|[1-9][0-9](\.\d)?|100(\.0)?/
                        }
                        placeholderText: "分值"
                        onTextEdited:{
                            updateZuJuanXinxi();
                        }
                    }
                    Label{
                        Layout.alignment: Qt.AlignLeft
                        Layout.rightMargin: 10
                        font.pointSize: 12
                        text:"分"
                    }
                    //多选题设置
                    Label{
                        Layout.leftMargin: 10
                        text: "多选题"
                        leftPadding: 10
                        rightPadding: 10
                        topPadding: 5
                        bottomPadding: 5
                        font.pointSize: 14
                        background: Rectangle{
                            color: Material.color(Material.DeepPurple)
                        }
                        Material.foreground: "white"
                    }
                    Label{
                        font.pointSize: 12
                        text: "共"+tikuNum["多选题"]+"题"
                    }
                    TextField{
                        id:dxnum
                        Layout.fillWidth: true
                        Layout.leftMargin: 20
                        font.pointSize: 12
                        horizontalAlignment: Text.AlignHCenter
                        selectByMouse: true
                        validator: IntValidator{bottom: 0; }
                        color: parseInt(text)>tikuNum["多选题"] ? "red" : "black"
                        placeholderText: "出题数量"
                        onTextEdited:{
                            updateZuJuanXinxi();
                        }
                    }
                    Label{
                        Layout.alignment: Qt.AlignLeft
                        font.pointSize: 12
                        text:"题"
                    }
                    TextField{
                        id:dxfenzhi
                        Layout.fillWidth: true
                        Layout.leftMargin: 20
                        font.pointSize: 12
                        horizontalAlignment: Text.AlignHCenter
                        selectByMouse: true
                        validator:RegExpValidator{
                            regExp: /\d(\.\d)?|[1-9][0-9](\.\d)?|100(\.0)?/
                        }
                        placeholderText: "分值"
                        onTextEdited:{
                            updateZuJuanXinxi();
                        }
                    }
                    Label{
                        Layout.alignment: Qt.AlignLeft
                        Layout.rightMargin: 10
                        font.pointSize: 12
                        text:"分"
                    }

                    //判断题
                    Label{
                        Layout.leftMargin: 10
                        text: "判断题"
                        leftPadding: 10
                        rightPadding: 10
                        topPadding: 5
                        bottomPadding: 5
                        font.pointSize: 14
                        background: Rectangle{
                            color: Material.color(Material.DeepPurple)
                        }
                        Material.foreground: "white"
                    }
                    Label{
                        font.pointSize: 12
                        text: "共"+tikuNum["判断题"]+"题"
                    }
                    TextField{
                        id:pnum
                        Layout.fillWidth: true
                        Layout.leftMargin: 20
                        font.pointSize: 12
                        horizontalAlignment: Text.AlignHCenter
                        selectByMouse: true
                        validator: IntValidator{bottom: 0; }
                        color: parseInt(text)>tikuNum["判断题"] ? "red" : "black"
                        placeholderText: "出题数量"
                        onTextEdited:{
                            updateZuJuanXinxi();
                        }
                    }
                    Label{
                        Layout.alignment: Qt.AlignLeft
                        font.pointSize: 12
                        text:"题"
                    }
                    TextField{
                        id:pfenzhi
                        Layout.fillWidth: true
                        Layout.leftMargin: 20
                        font.pointSize: 12
                        horizontalAlignment: Text.AlignHCenter
                        selectByMouse: true
                        validator:RegExpValidator{
                            regExp: /\d(\.\d)?|[1-9][0-9](\.\d)?|100(\.0)?/
                        }
                        placeholderText: "分值"
                        onTextEdited:{
                            updateZuJuanXinxi();
                        }
                    }
                    Label{
                        Layout.alignment: Qt.AlignLeft
                        Layout.rightMargin: 10
                        font.pointSize: 12
                        text:"分"
                    }

                    //填空题
                    Label{
                        Layout.leftMargin: 10
                        text: "填空题"
                        leftPadding: 10
                        rightPadding: 10
                        topPadding: 5
                        bottomPadding: 5
                        font.pointSize: 14
                        background: Rectangle{
                            color: Material.color(Material.DeepPurple)
                        }
                        Material.foreground: "white"
                    }
                    Label{
                        font.pointSize: 12
                        text: "共"+tikuNum["填空题"]+"题"
                    }
                    TextField{
                        id:tnum
                        Layout.fillWidth: true
                        Layout.leftMargin: 20
                        font.pointSize: 12
                        horizontalAlignment: Text.AlignHCenter
                        selectByMouse: true
                        validator: IntValidator{bottom: 0; }
                        color: parseInt(text)>tikuNum["填空题"] ? "red" : "black"
                        placeholderText: "出题数量"
                        onTextEdited:{
                            updateZuJuanXinxi();
                        }
                    }
                    Label{
                        Layout.alignment: Qt.AlignLeft
                        font.pointSize: 12
                        text:"题"
                    }
                    TextField{
                        id:tfenzhi
                        Layout.fillWidth: true
                        Layout.leftMargin: 20
                        font.pointSize: 12
                        horizontalAlignment: Text.AlignHCenter
                        selectByMouse: true
                        validator:RegExpValidator{
                            regExp: /\d(\.\d)?|[1-9][0-9](\.\d)?|100(\.0)?/
                        }
                        placeholderText: "分值"
                        onTextEdited:{
                            updateZuJuanXinxi();
                        }
                    }
                    Label{
                        Layout.alignment: Qt.AlignLeft
                        Layout.rightMargin: 10
                        font.pointSize: 12
                        text:"分"
                    }
                    //简答题
                    Label{
                        Layout.leftMargin: 10
                        text: "简答题"
                        leftPadding: 10
                        rightPadding: 10
                        topPadding: 5
                        bottomPadding: 5
                        font.pointSize: 14
                        background: Rectangle{
                            color: Material.color(Material.DeepPurple)
                        }
                        Material.foreground: "white"
                    }
                    Label{
                        font.pointSize: 12
                        text: "共"+tikuNum["简答题"]+"题"
                    }
                    TextField{
                        id:jnum
                        Layout.fillWidth: true
                        Layout.leftMargin: 20
                        font.pointSize: 12
                        horizontalAlignment: Text.AlignHCenter
                        selectByMouse: true
                        validator: IntValidator{bottom: 0; }
                        color: parseInt(text)>tikuNum["简答题"] ? "red" : "black"
                        placeholderText: "出题数量"
                        onTextEdited:{
                            updateZuJuanXinxi();
                        }
                    }
                    Label{
                        Layout.alignment: Qt.AlignLeft
                        font.pointSize: 12
                        text:"题"
                    }
                    TextField{
                        id:jfenzhi
                        Layout.fillWidth: true
                        Layout.leftMargin: 20
                        font.pointSize: 12
                        horizontalAlignment: Text.AlignHCenter
                        selectByMouse: true
                        //要求输入0-100之间的浮点数，小数点后保留一位
                        //如果是<10的浮点，0.0-9.9之间的数字;如果是>=10,<100的数字，10.0-99.9;允许输入100或100.0
                        validator:RegExpValidator{
                            regExp: /\d(\.\d)?|[1-9][0-9](\.\d)?|100(\.0)?/
                        }

                        placeholderText: "分值"
                        onTextEdited:{
                            updateZuJuanXinxi();
                        }
                    }
                    Label{
                        Layout.alignment: Qt.AlignLeft
                        Layout.rightMargin: 10
                        font.pointSize: 12
                        text:"分"
                    }
                }

            }

            //组卷情况
            Rectangle{
                Layout.fillWidth: true
                Layout.leftMargin: 10
                Layout.rightMargin: 10
                implicitHeight: ly5.height+10
                color: "white"
                radius: 5
                ColumnLayout{
                    id:ly5
                    width: parent.width
                    Label{
                        Layout.topMargin: 10
                        Layout.leftMargin: 10
                        Layout.alignment: Qt.AlignLeft
                        font.pointSize: 16
                        text:"组卷情况"
                    }
                    TextField{
                        id:txt_zuJuanXinXi
                        Layout.leftMargin: 10
                        Layout.fillWidth: true
                        Layout.rightMargin: 10
                        font.pointSize: 12
                        placeholderText: "本试卷共XX题，总分值XX分"
                    }
                }
            }

            //试卷页面设置
            Rectangle{
                Layout.fillWidth: true
                Layout.leftMargin: 10
                Layout.rightMargin: 10
                implicitHeight: ly6.height+10
                color: "white"
                radius: 5
                GridLayout{
                    id:ly6
                    width: parent.width
                    columns: 4
                    Label{
                        Layout.topMargin: 10
                        Layout.leftMargin: 10
                        Layout.columnSpan: 1
                        font.pointSize: 16
                        text:"页面设置"
                    }
                    Button{
                        Layout.alignment: Qt.AlignLeft
                        Layout.topMargin: 10
                        Layout.rightMargin: 10
                        Layout.columnSpan: 3
                        text: "高级设置"
                        Material.background: Material.Green
                        Material.foreground: "white"
                        onClicked: {
                            function getPageSettings(){
                                //保存设置信息到文件
                                pageSettingMap = myLoader.item.pageSettingMap;
                                var jsonString = JSON.stringify(pageSettingMap);//把对象转为字符串
//                                console.log("获取值",jsonString);
                                settingsPage.setValue("pageSet",jsonString);
                                myLoader.item.onAccepted.disconnect(getPageSettings);
                                myLoader.item.onError.disconnect(setError);
                                myLoader.item.onSucess.disconnect(setSucess);
                                myLoader.source = "";
                            }
                            //显示错误消息
                            function setError(erorMsg){
                                messageBox.close();//如果已经打开就先关闭
                                messageBox.text = erorMsg;
                                messageBox.style = "danger";
                                messageBox.open();
                            }
                            //显示成功消息
                            function setSucess(){
                                messageBox.close();//如果已经打开就先关闭
                                messageBox.text = "页面设置成功！";
                                messageBox.style = "sucess";
                                if(!timer.running){
                                    timer.start();
                                }
                                else{
                                    timer.stop();
                                    timer.start();
                                }
                                messageBox.open();
                            }

                            //显示设置对话框
                            myLoader.sourceComponent = dg_pageSetting;
                            pageSettingMap = readPageSetting();
                            myLoader.item.pageSettingMap = pageSettingMap;
                            myLoader.item.paperSize = cb_pageSize.currentText;
                            myLoader.item.isZhangDing = rb_side.checked;
                            myLoader.item.open();
                            myLoader.item.onAccepted.connect(getPageSettings);
                            myLoader.item.onError.connect(setError);
                            myLoader.item.onSucess.connect(setSucess);
                        }
                    }

                    DashLineHShape{
                        id:root
                        Layout.preferredHeight: 5
                        Layout.leftMargin: 10
                        Layout.rightMargin: 10
                        Layout.columnSpan :4
                        Layout.fillWidth: true
                    }

                    RadioButton{
                        id:rb_doubleSide
                        Layout.leftMargin: 10
                        Layout.columnSpan :1
                        font.pointSize: 12
                        checked: false
                        autoExclusive:false
                        text: "双面打印"
                    }
                    RadioButton{
                        id:rb_side
                        Layout.leftMargin: 10
                        Layout.columnSpan :1
                        font.pointSize: 12
                        checked: false
                        autoExclusive:false
                        text: "考生信息在侧面"
                    }
                    Label{
                        Layout.alignment: Qt.AlignRight
                        Layout.columnSpan: 1
                        font.pointSize: 12
                        text:"纸张大小"
                    }
                    ComboBox{
                        id:cb_pageSize
                        Layout.rightMargin: 10
                        model: ["A4","A3"]
                        currentIndex: 0
                    }
                }
            }

            //生成试卷按钮
            Button{
                id:bt_printTestPaper
                Layout.alignment: Qt.AlignHCenter | Qt.AlignTop
                Layout.bottomMargin: 20
                Layout.preferredHeight: 60
                font.pointSize: 16
                text: "生成试卷"
                Material.background: Material.Green
                Material.foreground: "white"
                enabled: false
                onClicked: {
                    //检查组卷信息的正确性
                    checkZuJuan();
                    if(hasError || hasWarning){
                        dg_confirm.open();
                    }else
                        printTestPaper();
                }
            }

        }
        ScrollBar.vertical: ScrollBar { }
    }
    Loader{id:myLoader;anchors.fill: parent}
    Component{
        id:openFileDialog
        FileDialog {
            title: "选择题库文件"
            folder: "file:///F:/temp/"
            nameFilters: [ "Excel 文件 (*.xls *.xlsx)", "All files (*)" ]
        }
    }
//    Component.onCompleted:{
//        let appPath = Qt.application.arguments[0];
//        appDir = appPath.substring(0,appPath.lastIndexOf('\\'))
//    }
    //保存设置信息
    Settings{
        id:settingsMain
        //单选题
        property alias d_num: dnum.text //保存了单选题的数量
        property alias d_fenzhi: dfenzhi.text //保存单选题分值
        //多选题
        property alias dx_num: dxnum.text //保存了单选题的数量
        property alias dx_fenzhi: dxfenzhi.text //保存单选题分值
        //判断题
        property alias p_num: pnum.text //保存了单选题的数量
        property alias p_fenzhi: pfenzhi.text //保存单选题分值
        //填空题
        property alias t_num: tnum.text //保存了单选题的数量
        property alias t_fenzhi: tfenzhi.text //保存单选题分值
        //简答题
        property alias j_num: jnum.text //保存了单选题的数量
        property alias j_fenzhi: jfenzhi.text //保存单选题分值
        //页面设置
        property alias pageSize: cb_pageSize.currentIndex //纸张
        property alias doubleSide: rb_doubleSide.checked //是否双面
        property alias side: rb_side.checked //考生信息是否在侧面
        //导入文件的目录
        property var importFoldPath

        fileName : appDir + "\/testPageSet.ini"

    }
    //保存高级页面设置信息
    Settings{
        id:settingsPage
        category: "pageSetting"
        fileName: appDir+"\/testPageSet.ini"
        Component.onCompleted:{
            pageSettingMap = readPageSetting();
        }
    }
    //消息框
    MessageBox{
        id:messageBox
        anchors.top: parent.top
        implicitWidth: parent.width-10
        anchors.horizontalCenter: parent.horizontalCenter
        visible: false
        Timer {//用于定时关闭成功信息
            id:timer
            interval: 3000
            running: false
            repeat: false
            onTriggered:
                messageBox.close();
        }
    }
    //关于
    Dialog{
        id:dg_about
        //设置位置
        anchors.centerIn: parent
//        width : parent.width*1/2
        Material.background: "white"
        modal: true
        closePolicy: Popup.NoAutoClose //只能手动关闭
        header: Label {
            leftPadding: 10
            topPadding: 10
            bottomPadding: 10
            font.family: Fonts.solidIcons
            font.pointSize: 18
            color: "#a94442"
            text:"\uf071"
            background: Rectangle{
                color: "#f8f8ff"
            }
        }
        contentItem:ColumnLayout{
            spacing: 10
            Label{
                text: "版权所用"
                font.bold: true
                font.pointSize: 14
            }
            Text {
                Layout.fillWidth: true
                font.pointSize: 12
                wrapMode: Text.Wrap
                text: qsTr("陈先生13059852510")
            }
            DashLineH{
                Layout.fillWidth: true
            }
            Label{
                text: "使用说明："
                font.pointSize: 14
                font.bold: true
            }
            Text {
                Layout.fillWidth: true
                font.pointSize: 12
                wrapMode: Text.Wrap
                text: qsTr("1.必须使用Excel作为题库\n\r2.题库所在工作表名称必须为“题库”\n\r3.设置成横向页面后，自动分2栏显示")
            }

        }
        footer: RowLayout{
            Item{
                Layout.fillWidth: true
            }
            DialogButtonBox {
                Button {
                    Material.background: Material.Green
                    Material.foreground: "white"
                    text: qsTr("确定")
                    DialogButtonBox.buttonRole: DialogButtonBox.YesRole
                    visible: true
                    onClicked: {
                        dg_about.close();
                    }
                }
            }
        }
        visible: false
    }
    Shortcut{
        autoRepeat: false
        context: Qt.ApplicationShortcut
        sequence:"F1"
        onActivated: {
            dg_about.open();
        }

    }

    //确认对话框
    Dialog{
        id:dg_confirm
        //设置位置
        anchors.centerIn: parent
        width : parent.width*2/3
        Material.background: "white"
        modal: true
        closePolicy: Popup.NoAutoClose //只能手动关闭
        header: Label {
            leftPadding: 10
            topPadding: 10
            bottomPadding: 10
            font.family: Fonts.solidIcons
            font.pointSize: 18
            color: "#a94442"
            text:"\uf071"
            background: Rectangle{
                color: "#f8f8ff"
            }
        }
        contentItem:ColumnLayout{
            spacing: 5
            Label{
                text: "警告:"
                font.bold: true
                font.pointSize: 14
                visible: hasWarning
            }
            Text {
                id: txt_warningMsg
                Layout.fillWidth: true
                visible: hasWarning
                font.pointSize: 12
                wrapMode: Text.Wrap
                text: qsTr("警告信息")
            }
            DashLineH{
                Layout.fillWidth: true //必须设置，否则不显示
                visible: hasWarning && hasError
            }
            Label{
                text: "组题设置存在致命错误:"
                font.pointSize: 14
                font.bold: true
                visible: hasError
            }
            Text {
                id: txt_errorMsg
                Layout.fillWidth: true
                font.pointSize: 12
                wrapMode: Text.Wrap
                visible: hasError
                text: qsTr("错误信息")
            }


        }
        footer: RowLayout{
            Item{
                Layout.fillWidth: true
            }
            DialogButtonBox {
                Button {
                    Material.background: Material.Green
                    Material.foreground: "white"
                    text: qsTr("仍然打印")
                    DialogButtonBox.buttonRole: DialogButtonBox.YesRole
                    visible: !hasError //如果存在严重错误就不能打印
                    onClicked: {
                        printTestPaper();
                        dg_confirm.close();
                    }
                }
            }
            DialogButtonBox {
                Button {
                    Material.background: Material.Green
                    Material.foreground: "white"
                    text: qsTr("取消")
                    DialogButtonBox.buttonRole:  DialogButtonBox.NoRole
                    onClicked: {
                        dg_confirm.close();
                    }
                }
            }
        }

        visible: false
    }
    //页面设置对话框
    Component{
        id: dg_pageSetting
        PageSetting{
            anchors.centerIn: parent
            implicitWidth: 500
        }
    }

}
